package com.woniuxy.productServer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.util.StateUtil;
import com.woniuxy.productServer.dto.MovieInfoDto;
import com.woniuxy.productServer.exception.movieInfo.MovieException;
import com.woniuxy.productServer.exception.movieInfo.MovieExceptionCode;
import com.woniuxy.productServer.form.movie.MovieAuthForm;
import com.woniuxy.productServer.model.MovieInfo;
import com.woniuxy.productServer.dao.MovieInfoMapper;
import com.woniuxy.productServer.param.movie.MovieAddParam;
import com.woniuxy.productServer.param.movie.MovieChangeParam;
import com.woniuxy.productServer.param.movie.MovieLoadParam;
import com.woniuxy.productServer.service.MovieInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.productServer.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年03月20日
 */
@Service
public class MovieInfoServiceImpl extends ServiceImpl<MovieInfoMapper, MovieInfo> implements MovieInfoService {

    @Autowired
    private MovieInfoMapper movieInfoMapper;
    @Autowired
    private OssUtil ossUtil;

    @Override
    public MovieInfoDto add(MovieAddParam param) throws Exception{
        //判断
        MovieInfo movieInfo = movieInfoMapper.selectOne(new QueryWrapper<MovieInfo>().eq("movie_name", param.getMovieName()));
        if (movieInfo!=null) throw new MovieException(MovieExceptionCode.MOVIE_HAD_EXIST);
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date movieUpdate = simpleFormat.parse(param.getMovieUpdate());
        Date movieDowndate = simpleFormat.parse(param.getMovieDowndate());
        if (movieUpdate.after(movieDowndate)
                ||movieDowndate.before(new Date())
                ||(movieUpdate.getTime()+param.getMovieTime()*60*1000)>=movieDowndate.getTime())
            throw new MovieException(MovieExceptionCode.MOVIE_DATE_ERROR);

        MovieInfo movieInfo1 = BeanUtil.toBean(param, MovieInfo.class);
        movieInfo1.setMovieType(JSONUtil.toJsonStr(param.getMovieTypes()));
        movieInfo1.setMovieActor(JSONUtil.toJsonStr(param.getMovieActors()));
        save(movieInfo1);
        MovieInfoDto movieInfoDto = BeanUtil.toBean(movieInfo1, MovieInfoDto.class);
        movieInfoDto.setMovieActors(param.getMovieActors());
        movieInfoDto.setMovieTypes(param.getMovieTypes());
        movieInfoDto.setMovieMoney(new BigDecimal(0));
        movieInfoDto.setMovieScore(new BigDecimal(5));
        return movieInfoDto;
    }

    @Override
    public void change(MovieChangeParam param) {
        MovieInfo movieInfo = getById(param.getMovieId());
        if (movieInfo==null) throw new MovieException(MovieExceptionCode.MOVIE_NOT_EXIST);
        if (movieInfo.getMovieStatus()==param.getMovieState()) throw new MovieException(MovieExceptionCode.MOVIE_STATE_ERROR);
        movieInfo.setMovieStatus(param.getMovieState());
        updateById(movieInfo);
    }

    @Override
    public PageInfo<MovieInfoDto> load(MovieLoadParam param) {
        PageHelper.startPage(param.getPageNum()==null?1:param.getPageNum(),param.getPageSize()==null?3:param.getPageSize());
        QueryWrapper<MovieInfo> q = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getMovieName())) q.like("movie_name",param.getMovieName());
        if (ObjectUtil.isNotEmpty(param.getOrder())) {
            if (param.getOrder()== StateUtil.MOVIE_ORDER_BY_MONEY) q.orderByDesc("movie_money");
            if (param.getOrder()== StateUtil.MOVIE_ORDER_BY_TIME) q.orderByDesc("movie_update");
        }
        if (param.getTypes().size()!=0) {
            for (String type : param.getTypes()) {
                q.like("movie_type",type);
            }
        }
        List<MovieInfo> list = list(q);
        PageInfo<MovieInfo> pageInfo = new PageInfo<>(list);
        List<MovieInfoDto> movieInfoDtos = new ArrayList<>();
        for (MovieInfo movieInfo : list) {
            MovieInfoDto movieInfoDto = BeanUtil.toBean(movieInfo, MovieInfoDto.class);
            movieInfoDto.setMovieTypes(JSONUtil.toList(movieInfo.getMovieType(),String.class));
            movieInfoDto.setMovieActors(JSONUtil.toList(movieInfo.getMovieActor(), MovieAuthForm.class));
            movieInfoDtos.add(movieInfoDto);
        }
        PageInfo<MovieInfoDto> pageInfo1 = new PageInfo<>(movieInfoDtos);
        pageInfo1.setTotal(pageInfo.getTotal());
        pageInfo1.setPageSize(pageInfo.getPageSize());
        pageInfo1.setPageNum(pageInfo.getPageNum());
        pageInfo1.setPages(pageInfo.getPages());
        return pageInfo1;
    }

//    public static void main(String[] args) {
//        ArrayList<String> strings = new ArrayList<String>();
//        strings.add("11");
//        strings.add("22");
//        String s = JSONUtil.toJsonStr(strings);
//        List<String> strings1 = JSONUtil.toList(s, String.class);
//        System.out.println(strings1);
//    }
}
